package com.atguigu.tingshu.user.service.impl;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserListenProcess;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.user.service.UserListenProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserListenProcessServiceImpl implements UserListenProcessService {

	@Autowired
	private MongoTemplate mongoTemplate;

	@Autowired
	private UserInfoService userInfoService;



	//获取当前声音的进度
	@Override
	public BigDecimal getTrackBreakSecond(Long TrackId) {
		String CollectName = "trackInfo_" + TrackId;

		//判断当前用户时都收听过当前声音
		UserListenProcess userListenProcesses = mongoTemplate.findOne(
				//添加查询条件
				new Query(Criteria.where("userId").is(AuthContextHolder.getUserId())
				.and("trackId").is(TrackId)),
				//返回值类型 和 集合名字
				UserListenProcess.class, CollectName);
		//如果userListenProcesses不为空
		if (userListenProcesses != null){
			//说明听过该声音,返回声音进度
			return userListenProcesses.getBreakSecond();
		}
		//返回进度0
		return new BigDecimal(0);
	}
}
